package com.closer.server.mapper.user;

import com.closer.server.biz.user.dto.SearchRequest;
import com.closer.server.biz.user.dto.vo.UserVO;
import com.closer.server.support.sql.QueryWrapperHelper;
import com.mybatisflex.core.BaseMapper;
import com.closer.server.biz.user.entity.User;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;

import java.util.List;

/**
 * 用户 映射层。
 *
 * @author closer
 */
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据账号查询用户是否存在
     * @param account 账号
     * @return 存在返回1，不存在返回0
     */
    default Long existByAccount(String account){
        var queryWrapper = new QueryWrapper().eq("account", account);
        return selectCountByQuery(queryWrapper);
    }

    /**
     * 根据账号和密码查询用户信息
     * @param account 账号
     * @param password 密码
     * @return 存在返回用户信息，不存在返回null
     */
    default UserVO getUserInfo(String account, String password){
        var queryWrapper = QueryWrapper.create()
                .select("user.*", "user_statistics.solved", "user_statistics.submit", "role.code as role")
                .from("user")
                .leftJoin("user_statistics").on("user_statistics.user_id = user.id")
                .leftJoin("user_role").on("user_role.user_id = user.id")
                .leftJoin("role").on("role.id = user_role.role_id")
                .eq("user.account", account)
                .eq("user.password", password);
        return selectOneByQueryAs(queryWrapper, UserVO.class);
    }

    /**
     * 根据用户id查询用户信息
     * @param userId 用户id
     * @return 存在返回用户信息，不存在返回null
     */
    default UserVO getUserInfoById(Long userId){
        var queryWrapper = QueryWrapper.create()
                .select("user.*", "user_statistics.solved", "user_statistics.submit", "role.code as role")
                .from("user")
                .leftJoin("user_statistics").on("user_statistics.user_id = user.id")
                .leftJoin("user_role").on("user_role.user_id = user.id")
                .leftJoin("role").on("role.id = user_role.role_id")
                .eq("user.id", userId);
        return selectOneByQueryAs(queryWrapper, UserVO.class);
    }

    /**
     * 根据用户id查询用户角色列表
     * @param loginId 用户id
     * @return 角色列表
     */
    default List<String> getRoleList(Object loginId){
        var queryWrapper = QueryWrapper.create()
                .select("role.code")
                .from("user_role")
                .leftJoin("role").on("role.id = user_role.role_id")
                .eq("user_role.user_id", loginId);
        return selectListByQueryAs(queryWrapper, String.class);
    }

    /**
     * 根据查询参数查询用户列表
     * @param params 查询参数
     * @return 用户列表
     */
    default Page<UserVO> getUserVOList(SearchRequest params){
        var queryWrapper = QueryWrapper.create()
                .select("user.*", "user_statistics.solved", "user_statistics.submit", "role.code as role")
                .from("user")
                .leftJoin("user_statistics").on("user_statistics.user_id = user.id")
                .leftJoin("user_role").on("user_role.user_id = user.id")
                .leftJoin("role").on("role.id = user_role.role_id")
                .orderBy("user.create_time desc");
                QueryWrapperHelper.likeIfPresent(queryWrapper, params.getAccount(), "user.account");
                QueryWrapperHelper.likeIfPresent(queryWrapper, params.getId(), "user.id");
                QueryWrapperHelper.likeIfPresent(queryWrapper, params.getGender(), "user.gender");
        return paginateAs(params.getPageNumber(), params.getPageSize(),queryWrapper, UserVO.class);

    }
}